package com.study.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.transaction.Synchronization;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.model.pojo.Anniversary;
import com.study.model.pojo.Customer;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Linkman;
import com.study.services.AnniversaryService;
import com.study.services.CareService;
import com.study.services.CustomerService;
import com.study.services.DictionaryService;
import com.study.services.LinkmanService;

@Controller
@RequestMapping("/customer")
public class AnniversaryController {
	@Autowired
	AnniversaryService service;
	@Autowired
	CustomerService cus;
	@Autowired
	DictionaryService dis;
	@Autowired
	LinkmanService link;
	@Autowired
	CareService care;
	
	

	/**
	 * 默认跳转的分页查询
	 * @param session
	 * @return 
	 * @return 
	 */
	@PostMapping("/anniversaryList")
	@ResponseBody
	public String selectAll(HttpSession session) {
		Integer userid = (Integer) session.getAttribute("useid");
		List<JSONObject> list = service.selectBySel(1, 10, "", "",userid);
		PageInfo<JSONObject> pageInfo = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pageInfo);
		return json;
	}
	
	/**
	 * 分页查询
	 */
	@GetMapping("/anniversaryList")
	@ResponseBody
	public ModelAndView getForm4(HttpSession session) {
		ModelAndView mv = new ModelAndView("customer/anniversaryList");
		return mv;
	}
	
	/**
	 * 带条件查询
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public String getForm3(String input,
			String selects,
			int currentPage,
			int pagesize,
			HttpSession session,HttpServletRequest request) {
		System.out.println(currentPage+"currentPage");
		System.out.println(pagesize+"pagesize");
		System.out.println(input+"input");
		System.out.println(selects+"selects");
		Integer userid = (Integer) session.getAttribute("useid");
		List<JSONObject> list = service.selectBySel(currentPage, pagesize, input.trim(), selects.trim(),userid);
		PageInfo<JSONObject> pageInfo = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pageInfo);
		return json;
	}
	
	/**
	 * 删除纪念日
	 * @param ann_id
	 * @return
	 */
	@PostMapping("/delanni")
	@ResponseBody
	public String delete(Integer ann_id) {
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>");
		System.out.println(ann_id);
		service.delete(ann_id);
		return "/customer/anniversaryList";
	}
	
	/**
	 * 新增页面
	 * @param model
	 * @return
	 */
	@GetMapping("/anniversaryAdd")
	public ModelAndView add(Model model) {
		ModelAndView mv = new ModelAndView("/customer/anniversaryAdd");
		mv.addObject("anniversary", new JSONObject());
		return mv;
	}
	
	/**
	 * 新建联系人纪念日
	 * @param aname
	 * @param linkman
	 * @param annity
	 * @param date1
	 * @param remark
	 * @param remind
	 * @return
	 */
	@PostMapping("/subForm")
	@ResponseBody
	public ModelAndView mypost(Integer custid,Integer linId,
								Integer annity,
								Date date1,
								String remark,
								String remind) {
		Anniversary a1 = new Anniversary();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//String anndate = sdf.format(date1);
		//System.out.println(sdf.format(date1));
		System.out.println("当前日期"+new Date());
		System.out.println("客户名称："+custid+"--日期："+sdf.format(date1)+"--纪念日类型："+annity+"--联系人编号："+linId);
		Dictionary d1 = dis.findByDicId(annity);
		Customer c = cus.findById(custid);
		Linkman lin = link.findById(linId);
		a1.setAnnDate(date1);
		a1.setAnnRemark(remark);
		a1.setCustomer(c);
		a1.setDictionary(d1);
		a1.setLinkman(lin);
		a1.setAdel(1);
		a1.setCreatedate(new Date());
		service.insertAnn(a1);
		ModelAndView mv = new ModelAndView("/customer/anniversaryList");
		return mv;
	}
	
	/**
	 * 编辑后提交
	 * @param cus_id
	 * @param lin_id
	 * @param annity
	 * @param date1
	 * @param remark
	 * @param remind
	 * @param id
	 * @return
	 */
	@PostMapping("/editanniForm")
	@ResponseBody
	public String editanni(Integer custid,Integer linId,
			Integer annity,
			Date date1,
			String remark,
			String remind,String id) {
		System.out.println("商品id为："+id);
		boolean result = service.editanni(custid, linId, annity, date1, remark, remind, id);
		System.out.println(result+"编辑结果");
		if (result) {
			return "ok";
		}else {
			return "fail";
		}
	}
	
	
	/**
	 * 跳转编辑界面
	 * @param request
	 * @return
	 */
	@GetMapping("/anniEditor")
	public ModelAndView updateAnni(HttpServletRequest request) {
		String ann_id = request.getParameter("id");
		JSONObject anniversary = service.selectById(ann_id);
		ModelAndView mv = new ModelAndView("/customer/anniversaryAdd");
		mv.addObject("anniversary", anniversary);
		System.out.println("可以编辑纪念日吗？"+ann_id);
		return mv;
	}
}
